package com.greenline.search.test.test.queryanalysis;

import com.greenline.search.test.dao.AnalyzeObject;
import com.greenline.search.test.utils.AnalyzeJSONToObject;
import com.greenline.search.test.utils.FileUtils;
import com.greenline.search.test.utils.JSONStreamParserForAnalyze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-23
 * Time: 下午3:49
 * To change this template use File | Settings | File Templates.
 */
public class ClassifierAnalyze {
    private static Logger logger = LoggerFactory.getLogger(ClassifierAnalyze.class);
    private static String baseURL = "http://192.168.1.37:8098/analyze/?q=";

    public static void main(String[] args) throws IOException {
        String classPath = FileUtils.class.getResource("/").getPath();
        String filePath = classPath+"dic/user_keyword_20140115.txt";
        List<String> keywordList = FileUtils.readFileAsString(filePath,"GBK");
        long startTime = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        int count = 0;
        String doctor = "doctor", hospital = "hospital", tabString = "\t", blankString = " ";
        for(String keyword : keywordList){
            if(count++ % 1000 == 0){
                System.out.println("Deal with " + count + " keywords already.");
            }
            String url = baseURL + URLEncoder.encode(keyword,"UTF-8");
            List<AnalyzeObject> objectList = JSONStreamParserForAnalyze.parse(url);
            if(objectList.size() == 2){
                AnalyzeObject object1 = objectList.get(0);
                AnalyzeObject object2 = objectList.get(1);
                String type1 = object1.getType();
                String type2 = object2.getType();
                if(!type1.contains("class") && !type2.contains("class")){
                    sb.append(keyword).append(tabString);
                    sb.append(object1.getKey()).append(blankString).append(object2.getKey()).append(tabString);
                    sb.append(object1.getType()).append(blankString).append(object2.getType()).append(tabString);
                    sb.append("\n");
                }

            }
        }
        logger.info(sb.toString());
        System.out.println("test end...");
        long costTime = System.currentTimeMillis() - startTime;
        System.out.println("The totalTime is " + (costTime/1000) + " seconds.");
    }
}
